import axios from 'axios';

const useMock = false;
const baseURL = useMock ? 'http://rap2api.taobao.org/app/mock/278597/' : 'http://127.0.0.1:7001/';

export default function fetch(option = { url: '', method: 'get' }) {
  const userInfo = JSON.parse(localStorage.getItem('userInfo'));
  const token = userInfo && userInfo.token;
  // eslint-disable-next-line no-param-reassign
  const service = axios.create({
    baseURL,
    timeout: 5000,
    responseType: 'json',
    withCredentials: true,
    ...option,
    headers: {
      'Content-Type': 'application/json;charset=utf-8',
      token,
      ...option.headers,
    },
  });

  service.interceptors.response.use((response) => response.data);

  return service(option);
}
